package com.bitdf.txing.cart.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.TypeReference;
import com.bitdf.txing.common.utils.HttpUtils;
import com.bitdf.txing.common.utils.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lizhiwei
 * @date 2023/8/30 20:38:59
 * 注释：自定义权限验证接口扩展
 */
@Component
@Slf4j
@ConditionalOnProperty(value = "tx.deploy.mode", havingValue = "cloud")
public class StpInterfaceImpl implements StpInterface {

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的权限列表
        ArrayList<String> list = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        params.put("loginId", loginId.toString());
        HashMap<String, String> headers = new HashMap<>();
        headers.put("satoken", StpUtil.getTokenValue());
        try {
            HttpResponse response = HttpUtils.doGet("http://127.0.0.1:88", "/txing/user/roleperm/perms/get/loginId", "GET", headers, params);
            // 获取实体并将其转换为字符串
            HttpEntity entity = response.getEntity();
            String responseText = EntityUtils.toString(entity, "UTF-8");
            // 使用 Jackson 解析 JSON 并将其转换为 User 对象
            ObjectMapper objectMapper = new ObjectMapper();
            R r = objectMapper.readValue(responseText, R.class);
            List<String> data = r.getData(new TypeReference<List<String>>() {
            });
            list.addAll(data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("[鉴权 获取权限列表] ==> {}", list);
        return list;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的角色列表
        ArrayList<String> list = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        params.put("loginId", loginId.toString());
        HashMap<String, String> headers = new HashMap<>();
        headers.put("satoken", StpUtil.getTokenValue());
        try {
            HttpResponse response = HttpUtils.doGet("http://127.0.0.1:88", "/txing/user/roleperm/roles/get/loginId", "GET", headers, params);
            // 获取实体并将其转换为字符串
            HttpEntity entity = response.getEntity();
            String responseText = EntityUtils.toString(entity, "UTF-8");
            // 使用 Jackson 解析 JSON 并将其转换为 User 对象
            ObjectMapper objectMapper = new ObjectMapper();
            R r = objectMapper.readValue(responseText, R.class);
            List<String> data = r.getData(new TypeReference<List<String>>() {
            });
            list.addAll(data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("[鉴权 获取角色列表] ==> {}", list);
        return list;
    }

}
